package de.tabuma.formcreator.logik;

import de.tabuma.formcreator.*;
import de.tabuma.formcreator.entity.*;
import de.tabuma.formcreator.javascript.logik.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.util.*;
import java.util.logging.*;

/**
  * @author $Author: $
  * @version $Revision: $
  * @since $Date: $
*/
public class FormManagerFacade {

    private static final Logger log = Logger.getLogger(FormManagerFacade.class.getName());
    public static final String iframeName = "iframeName";


    public static String getAll(String frame,List<FormStructur> formStructurList, boolean editable) {
        String str = "";
        for(int i=0;i<formStructurList.size();i++) {
            FormStructur fs = formStructurList.get(i);
            str += "<!-- Start JavaScript Methods/Object for Object "+fs.getId()+" ("+fs.getName()+") -->\n";
            str += EntityFacade.getAllObject(fs);
            str += ControllerFacade.getAllController(frame, fs);
            str += ViewFacade.getAllView(fs, editable);
            str += "<!-- End JavaScript Methods/Object for Object "+fs.getId()+" ("+fs.getName()+") -->\n";
        }
        return str;
    }

    public static String getXForm(HttpServletRequest request, FormManager formularManager, String dataservlet, String frame, String cssLink, long projectID, String method) {

        log.fine("start getXForm with 0 seconds");
        long startTime = Calendar.getInstance().getTimeInMillis();

        //Laden der Strukturliste
        List<FormStructur> requestList = formularManager.getRequestObject().getFormularStrukturList();
        List<FormStructur> responseList = formularManager.getResponseObject().getFormularStrukturList();
        List<FormStructur> faultList = null;
        if(formularManager.getFaultObject()!=null) {
            faultList = formularManager.getFaultObject().getFormularStrukturList();
        }
        
        log.fine("loading Structur list :"+(Calendar.getInstance().getTimeInMillis() - startTime));

        String html = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
        html += "<html>\n";
        html += "\t<head>\n";
        html += "\t\t<title>"+formularManager.getName()+"</title>\n";
        html += "\t\t<link rel=\"stylesheet\" type=\"text/css\" href=\""+cssLink+"\"/>\n";

        html += "\t\t<script language=\"JavaScript\" type=\"text/javascript\">\n\n";
        html += "<!--\n";

        html += printIframe(frame, cssLink);

        log.fine("creating request objects.");
        html += getAll(frame, requestList, true);
        if(requestList.size()>0) {
            html += ControllerFacade.getSendDataController(requestList.get(0));
        }

        log.fine("creating response objects.");
        html += getAll(frame, responseList, false);

        if(requestList.size()==0) {
            html += printStart();
        }
        else {
            html += printWelcome(formularManager, requestList);
        }

        if(formularManager.getFaultObject()!=null) {
            log.fine("creating fault object.");
            html += getAll(frame, faultList, false);
        }

        //printing generall function to remove element in array
        html += EntityFacade.removeArray();

        html += LoggingFacade.getLoggingContext(frame, request);
        

        //print sending js methods
        html += SendFacade.getSendMethod(dataservlet, projectID, method);
        if(responseList.size()>0) {
            if(formularManager.getFaultObject()!=null) {
                html += SendFacade.getResponseMethod(responseList.get(0), faultList.get(0));
            }
            else {
                html += SendFacade.getResponseMethod(responseList.get(0), null);
            }
        }

        html += "//-->\n";
        html += "\t\t</script>\n";

        html += "\t</head>\n";
        html += "\t<body onLoad=\"printWelcome()\">\n";
        html += "\t\t<div class=\"mainframe\">\n";
        html += "\t\t\t<div class=\"objectframe\">"+formularManager.getRequestObject().getName()+"</div>\n";
        html += "\t\t\t<div class=\"iframe\"><iframe class=\"iframe\" name=\""+iframeName+"\"></iframe></div>\n";
        html += "\t\t</div>\n";

        if(request.getParameter(constants.JSLoggingMode)!=null) {
            html += LoggingFacade.getLoggingTextarea();
        }

        html += "\t</body>\n";
        html += "</html>\n";

        log.fine("ready:"+(Calendar.getInstance().getTimeInMillis() - startTime));

        return html;
    }

    public static String printWelcome(FormManager formularManager,List<FormStructur> list) {

        String welcomeLegend = constants.getValueFromProperties("de.tabuma.formcreator.welcome.legend");
        String welcomeText = constants.getValueFromProperties("de.tabuma.formcreator.welcome.text");
        String welcomeStartButton = constants.getValueFromProperties("de.tabuma.formcreator.welcome.startbutton");

        String str = "";
        str += "\tfunction printWelcome() {\n";
        str += "\t\tstr = '';\n";
        str += "\t\tstr += '<fieldset class=\"fieldset\">';\n";
        str += "\t\tstr += '<legend>"+welcomeLegend+"</legend>';\n";
        str += "\t\tstr += '"+welcomeText+" "+formularManager.getRequestObject().getName()+".<br/><br/>';\n";
        if(list.size()>0) {
            str += "\t\tstr += '<input type=\"button\" name=\"startButton\" value=\""+welcomeStartButton+"\" class=\"buttonClass\" onClick=\"javascript:parent.printView"+list.get(0).getId()+"()\"/>';";
        }
        str += "\t\tstr += '</fieldset>';\n";
        str += "\t\t"+JSLib.getPrintIframe()+"(str);\n";
        str += "}\n";
        return str;
    }

    public static String printStart() {
        String str = "";
        str += "\tfunction printWelcome() {\n";
        str += "\t\tstr = 'Starten Sie den Webservice in dem Sie auf Start klicken.<br/>';\n";
        str += "\t\tstr += '<input type=\"button\" name=\"startButton\" value=\"start\" class=\"buttonClass\" onClick=\"javascript:parent.sendData()\"/>';\n";
        str += "\t\t"+JSLib.getPrintIframe()+"(str);\n";
        str += "\t}\n";
        return str;
    }

    public static String printIframe(String frame, String cssLink) {
        String str = "";
        str += "\tfunction "+JSLib.getPrintIframe()+"(string) {\n";
        str += "\t\tstr = '';\n";
        str += "\t\tstr += '<html>\\n<head>\\n<title>dynamic content</title>\\n<link rel=\"stylesheet\" rev=\"stylesheet\" type=\"text/css\" href=\""+cssLink+"\">\\n</script>\\n</head>\\n<body>' + string + '</body></html>';\n";
        str += "\t\t" + frame + ".document.body.innerHTML=str;\n";
        str += "\t\tstylelink = " + frame + ".document.createElement(\"link\");\n";
        str += "\t\tstylelink.href = \""+cssLink+"\";\n";
        str += "\t\tstylelink.rel = \"stylesheet\";\n";
        str += "\t\t" + frame + ".document.getElementsByTagName(\"head\")[0].appendChild(stylelink);\n";
        str += "\t}\n";
        return str;
    }
}
